package Thread2;


/**
 * 银行一个账户，两个储户分别向同一个账户存3000元
 * 每次存1000，存3次，存完打印余额
 */
public class Exer1 {
    public static void main(String[] args) {
        //创建一个公共的账户
         Account acct = new Account();
         //两个客户线程共享同一个账户
         KeHu k1 = new KeHu(acct);
         KeHu k2 = new KeHu(acct);
         k1.setName("储户1");
         k2.setName("储户2");
         k1.start();
         k2.start();
    }
}
class Account{
  private  int balance = 0;
  public synchronized void add(double money){
      if(money>0){
          notify();
          balance+= money;
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }

          System.out.println(Thread.currentThread().getName()+"存钱成功，余额为："+balance);
          try {
              wait();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }

  }
}

class KeHu extends Thread{
    private Account acct;

    public KeHu(Account acct) {
        this.acct = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i <3 ; i++) {
           acct.add(1000);
        }
    }
}